package com.example.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.database.DatabaseHelper;
import com.example.pojo.CourseOption;
import com.example.student.R;

import java.util.List;

public class CourseOptionAdapter extends RecyclerView.Adapter<CourseOptionAdapter.CourseViewHolder>{
    private List<CourseOption> courseList;
    private Context context;

    private DatabaseHelper databaseHelper;

    public CourseOptionAdapter(List<CourseOption> courseList, Context context, DatabaseHelper databaseHelper){
        this.context = context;
        this.courseList = courseList;
        this.databaseHelper = databaseHelper;
    }
    @NonNull
    @Override
    public CourseOptionAdapter.CourseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_course_1, parent, false);
        return new CourseViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull CourseOptionAdapter.CourseViewHolder holder, int position) {
        CourseOption courseOption = courseList.get(position);
        holder.textViewCourseId.setText("课程号：" + courseOption.getCourse().getCourseId());
        holder.textViewCourseName.setText("课程名：" + courseOption.getCourse().getCourseName());
        holder.textViewTeacher.setText("授课老师：" + courseOption.getTeacher().getTeacherName());
        holder.textViewTime.setText("时间：" + courseOption.getNumWeek()+" " + courseOption.getWeek() +
                " " + courseOption.getStartTime() + "-" + courseOption.getEndTime());
        holder.textViewCollegeMajor.setText("学院：" + courseOption.getCourse().getCollege() + " 专业："+
                courseOption.getCourse().getMajor());

        // 删除按钮点击事件
        holder.btnDelete.setOnClickListener(v -> {
            // 弹出确认对话框
            new AlertDialog.Builder(context)
                    .setTitle("删除课程")
                    .setMessage("确定要删除该课程吗？")
                    .setPositiveButton("确定", (dialog, which) -> {
                        // 从列表中移除数据
                        courseList.remove(position);
                        notifyItemRemoved(position);
                        notifyItemRangeChanged(position, courseList.size());

                        databaseHelper.deleteCourseInTeach(courseOption.getCourse().getCourseId(), courseOption.getTeacher().getTeacherId());
                        Toast.makeText(holder.itemView.getContext(), "删除成功", Toast.LENGTH_SHORT).show();
                    })
                    .setNegativeButton("取消", null)
                    .show();
        });
    }

    @Override
    public int getItemCount() {
        return courseList.size(); // 返回 courseList 的大小
    }

    public static class CourseViewHolder extends RecyclerView.ViewHolder {
        TextView textViewCourseId, textViewCourseName, textViewTeacher, textViewTime, textViewCollegeMajor;
        ImageButton btnDelete;
        public CourseViewHolder(@NonNull View itemView) {
            super(itemView);
            textViewCourseId = itemView.findViewById(R.id.textViewCourseId);
            textViewCourseName = itemView.findViewById(R.id.textViewCourseName);
            textViewTeacher = itemView.findViewById(R.id.textViewTeacher);
            textViewTime = itemView.findViewById(R.id.textViewTime);
            textViewCollegeMajor = itemView.findViewById(R.id.textViewCollegeMajor);
            btnDelete = itemView.findViewById(R.id.btnDelete);
        }
    }
}
